﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manager_System
{
    public partial class intoWH_material : Form
    {
        string Id = "";
        string Species = "";
        string Weight = "";
        string Price = "";
        main_form form1;

        public intoWH_material(string id,string species,string weight,string price,main_form form)
        {
            InitializeComponent();
            Id = id;
            Species = species;
            Weight = weight;
            Price = price;
            form1 = form;
        }

        private void intoWH_material_Load(object sender, EventArgs e)
        {
            ID_tb.Text = Id;
            species_tb.Text = Species;
            after_tb.Text = Weight;
            price_tb.Text = Price;
        }

        private void weight_tb_TextChanged(object sender, EventArgs e)
        {
            if (this.after_tb.Text != null && this.weight_tb.Text != null && !this.after_tb.Text.Equals("") && !this.weight_tb.Text.Equals(""))
            {
                after_tb.Text = Weight;
                double weight = double.Parse(this.weight_tb.Text);
                double after = double.Parse(this.after_tb.Text);
                double w = double.Parse(Weight);
                after = w + weight;
                if (after <= 0)
                {
                    this.after_tb.Text = "0";
                    this.weight_tb.Text = Weight;
                }
                else
                {
                    this.after_tb.Text = after.ToString();
                }
            }
            else
            {
                this.after_tb.Text = Weight;
            }
        }

        private void intoWH_bt_Click(object sender, EventArgs e)
        {
            double price = double.Parse(this.price_tb.Text);
            double weight = double.Parse(this.after_tb.Text);
            string id = this.ID_tb.Text;
            string species = this.species_tb.Text;
            if (weight != 0)
            {
                string sql = "update T_WH_material set Weight=" + weight + " ,Species='" + species + "',Price=" + price + " where ID=" + id + "";
                DBhelp db = new DBhelp();
                int rs = db.update(sql);
                db.close();
                if (rs == 1)
                {
                    MessageBox.Show("出库成功！");
                }
                else
                {
                    MessageBox.Show("出库失败！");
                }
            }
            else
            {
                string sql = "delete from T_WH_material where ID=" + id + "";
                DBhelp db = new DBhelp();
                int rs = db.update(sql);
                db.close();
                if (rs == 1)
                {
                    MessageBox.Show("出库成功！");
                }
                else
                {
                    MessageBox.Show("出库失败！");
                }
            }
            form1.Refresh_warehouse_material_LV();
        }

        private void close_bt_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
